import pygame

# 初始化pygame
pygame.init()

# 定义常量
WIDTH = 800
HEIGHT = 600
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
PLAYER_SIZE = 50
PLAYER_SPEED = 5
ATTACK_DAMAGE = 10

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("格斗游戏")

# 玩家类
class Player:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.health = 100
        self.is_attacking = False

    def move_left(self):
        if self.x > 0:
            self.x -= PLAYER_SPEED

    def move_right(self):
        if self.x < WIDTH - PLAYER_SIZE:
            self.x += PLAYER_SPEED

    def move_up(self):
        if self.y > 0:
            self.y -= PLAYER_SPEED

    def move_down(self):
        if self.y < HEIGHT - PLAYER_SIZE:
            self.y += PLAYER_SPEED

    def attack(self, other):
        if self.is_attacking:
            distance = ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
            if distance < PLAYER_SIZE:
                other.health -= ATTACK_DAMAGE
                if other.health < 0:
                    other.health = 0
            self.is_attacking = False

    def draw(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, PLAYER_SIZE, PLAYER_SIZE))
        # 绘制生命值条
        health_bar_width = int((self.health / 100) * PLAYER_SIZE)
        pygame.draw.rect(screen, GREEN, (self.x, self.y - 10, health_bar_width, 5))

# 创建两个玩家
player1 = Player(100, HEIGHT // 2, RED)
player2 = Player(WIDTH - 150, HEIGHT // 2, GREEN)

# 游戏主循环
clock = pygame.time.Clock()
running = True
while running:
    screen.fill(WHITE)

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player1.move_left()
            elif event.key == pygame.K_RIGHT:
                player1.move_right()
            elif event.key == pygame.K_UP:
                player1.move_up()
            elif event.key == pygame.K_DOWN:
                player1.move_down()
            elif event.key == pygame.K_SPACE:
                player1.is_attacking = True
            if event.key == pygame.K_a:
                player2.move_left()
            elif event.key == pygame.K_d:
                player2.move_right()
            elif event.key == pygame.K_w:
                player2.move_up()
            elif event.key == pygame.K_s:
                player2.move_down()
            elif event.key == pygame.K_RETURN:
                player2.is_attacking = True

    # 进行攻击
    player1.attack(player2)
    player2.attack(player1)

    # 绘制玩家
    player1.draw()
    player2.draw()

    # 检查游戏是否结束
    if player1.health == 0:
        print("玩家 2 获胜！")
        running = False
    elif player2.health == 0:
        print("玩家 1 获胜！")
        running = False

    # 更新显示
    pygame.display.flip()
    clock.tick(60)

# 退出游戏
pygame.quit()